package com.ihr360.payroll.model.dto.salary.meta;

import com.ihr360.commons.model.type.FieldSource;
import com.ihr360.expr.engine.VariableID;
import com.ihr360.payroll.model.base.ITaxedSalaryField;
import com.ihr360.payroll.model.dto.salary.embeddable.SalaryFieldValue;
import com.ihr360.payroll.model.dto.salary.meta.system.SystemSalaryField;
import com.ihr360.payroll.model.dto.salary.profile.CompanySalaryProfile;
import com.ihr360.payroll.model.dto.salary.superclass.AbstractSalaryField;
import com.ihr360.payroll.model.vo.PayrollConfigVo;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/** 薪酬字段 */
@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"config_id","code"}))
@DynamicUpdate
public class SalaryField extends AbstractSalaryField<SalaryFieldValue> implements ITaxedSalaryField {

    /** 代码
     * 对于从系统中选择的字段代码应该和meta中的code相同，如果是用户自己添加的，code和name一致
     */
    @Column(nullable = false, length = 32,updatable = false)
    private String code;

    /** 工资表配置 */
    @ManyToOne(fetch = FetchType.LAZY)
    protected PayrollConfig config;

    /** 薪酬字段元信息 */
    @ManyToOne(fetch = FetchType.EAGER)
    protected SystemSalaryField meta;

    /** 公司薪资档案信息 */
    @ManyToOne(fetch = FetchType.EAGER)
    protected CompanySalaryProfile companySalaryProfile;


    /** 字段取值方式 */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    protected FieldSource source;

    /** 字段值限制 */
    @Embedded
    protected SalaryFieldValue valueMeta;

    protected boolean placeholder;

    public SalaryField() {
    }

    public PayrollConfigVo getConfigVo(){
        if(config != null) {
            return new PayrollConfigVo(config.getId());
        }
        return null;
    }

    public PayrollConfig getConfig() {
        return config;
    }

    public void setConfig(PayrollConfig config) {
        this.config = config;
    }

    public SystemSalaryField getMeta() {
        return meta;
    }

    public void setMeta(SystemSalaryField meta) {
        this.meta = meta;
    }

    public SalaryFieldValue getValueMeta() {
        return valueMeta;
    }

    public void setValueMeta(SalaryFieldValue valueMeta) {
        this.valueMeta = valueMeta;
    }

    public FieldSource getSource() {
        return source;
    }

    public void setSource(FieldSource source) {
        this.source = source;
    }

    public boolean isPlaceholder() {
        return placeholder;
    }

    public void setPlaceholder(boolean placeholder) {
        this.placeholder = placeholder;
    }
    @Override
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public CompanySalaryProfile getCompanySalaryProfile() {
        return companySalaryProfile;
    }

    public void setCompanySalaryProfile(CompanySalaryProfile companySalaryProfile) {
        this.companySalaryProfile = companySalaryProfile;
    }
    private transient VariableID variableID;
    public VariableID  toVariableId(){
        if (variableID!=null){
            return variableID;
        }
        return VariableID.build(null,code);
    }
}